home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’92 / NetWarmer / source / FileM.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-22  |  5.8 KB  |  328 lines  |  [TEXT/KAHL]

  1. /* FileM.c */
  2. /* Created 3/13/4 1:08 PM by AppMaker */
  3.  
  4.  
  5. #include "Globals.h"
  6. #include "ResourceDefs.h"
  7. #include "Dispatcher.h"
  8. #include "Miscellany.h"
  9.  
  10. #include "FileM.h"
  11.  
  12. #define NIL            0L
  13.  
  14. #define dialogTop        75                                   
  15. #define dialogLeft        85
  16.  
  17. short            numOpenTypes;
  18. SFTypeList        openTypeList;
  19.  
  20. /*----------*/
  21. void InitFileM ()
  22. {
  23.     numOpenTypes = 1;
  24.     openTypeList [0] = 'TEXT';
  25. } /*InitFileM*/
  26.  
  27. /*----------*/
  28. Boolean OkToOpen (fType)
  29. OSType            fType;
  30. {
  31.     short            i;
  32.     enum {searching, found, notFound}
  33.                     status;
  34.  
  35.     i = 0;
  36.     status = searching;
  37.     while (status == searching) {
  38.         if (i >= numOpenTypes) {
  39.             status = notFound;
  40.         } else {
  41.             if (fType == openTypeList [i]) {
  42.                 status = found;
  43.             } else {
  44.                 i++;
  45.             }
  46.         }
  47.     } /*while*/
  48.     return (status == found);
  49. } /*OkToOpen*/
  50.  
  51. /*----------*/
  52. static Boolean OpenAppFile   (short        vRefNum,
  53.                     Str255        fName,
  54.                     short        *fRefNum);
  55. static Boolean OpenAppFile   (vRefNum, fName, fRefNum)
  56. short        vRefNum;
  57. Str255        fName;
  58. short        *fRefNum;
  59. {
  60.     Boolean            okay;
  61.  
  62.   /* application-specific Open file */
  63.     okay = CheckOS (FSOpen (fName, vRefNum, fRefNum));
  64.     return (okay);
  65. } /*OpenAppFile*/
  66.  
  67. /*----------*/
  68. static void CloseAppFile (short    fRefNum);
  69. static void CloseAppFile (fRefNum)
  70. short        fRefNum;
  71. {
  72.     Boolean            okay;
  73.  
  74.   /* application-specific Close file */
  75.           if (cur->windowKind == 1) { /*1st or only window in set*/
  76.         okay = CheckOS (FSClose (fRefNum));
  77.     }
  78. } /*CloseAppFile*/
  79.  
  80. /*----------*/
  81. static void SaveAppFile (short    fRefNum);
  82. static void SaveAppFile (fRefNum)
  83. short        fRefNum;
  84. {
  85.   /* application-specific Save file */
  86.     cur->dirty = false;
  87. } /*SaveAppFile*/
  88.  
  89. /*----------*/
  90. static Boolean ReadAppFile (short        fRefNum);
  91. static Boolean ReadAppFile (fRefNum)
  92. short        fRefNum;
  93. {
  94.   /* application-specific Read file */
  95.       return (false);
  96. } /*ReadAppFile*/
  97.  
  98. /*----------*/
  99. static void DoNew (void);
  100. static void DoNew ()
  101. {
  102.     Str255        untitled;
  103.  
  104.     BlockMove (&(**GetString (UntitledID)), untitled, 256);
  105.     OpenWindows (untitled, 0, 0);
  106. } /*DoNew*/
  107.  
  108. /*----------*/
  109. void OpenFile (fileName, vRefNum)
  110. Str255        fileName;
  111. short        vRefNum;
  112. {
  113.     short        fRefNum;
  114.  
  115.     if (OpenAppFile (vRefNum, fileName, &fRefNum)) {
  116.         OpenWindows (fileName, vRefNum, fRefNum);
  117.     }    
  118. } /*OpenFile*/
  119.  
  120. /*----------*/
  121. static void DoOpen (void);
  122. static void DoOpen ()
  123. {
  124.     Point            dialogOrigin;
  125.     SFReply            sfInfo;
  126.  
  127.     SetPt (&dialogOrigin, dialogLeft, dialogTop);
  128.     SFGetFile (dialogOrigin, "", nil, numOpenTypes, &openTypeList, nil, &sfInfo);
  129.     if (sfInfo.good) {
  130.         OpenFile (sfInfo.fName, sfInfo.vRefNum);
  131.     }
  132. } /*DoOpen*/
  133.  
  134. /*----------*/
  135. void Open0Files ()
  136. {
  137.     DoNew ();
  138. } /*Open0Files*/
  139.  
  140. /*----------*/
  141. static void DoSaveAs (void);
  142. static void DoSaveAs ()
  143. {
  144.     SFReply            sfInfo;
  145.     short            fRefNum;
  146.     StringHandle    prompt;
  147.     Str255            suggestion;
  148.     StringHandle    untitled;
  149.     
  150.     prompt = GetString (SaveAsPromptID);
  151.     suggestion [0] = 0;
  152.  
  153.     if (CreateFile (&sfInfo, *prompt, suggestion, 'XXXX', 'TEXT')) {
  154.         if (cur->fileNum != 0) {
  155.             CloseAppFile (cur->fileNum);
  156.         }
  157.         if (OpenAppFile (sfInfo.vRefNum, sfInfo.fName, &fRefNum)) {
  158.             SetWTitle (curWindow, sfInfo.fName);
  159.             cur->fileNum = fRefNum;
  160.             cur->volNum = sfInfo.vRefNum;
  161.             SaveAppFile (cur->fileNum);
  162.         } else { /*should never happen*/
  163.             untitled = GetString (UntitledID);
  164.             SetWTitle (curWindow, *untitled);
  165.             cur->fileNum = 0;
  166.             cur->volNum = 0;
  167.         }
  168.     }
  169. } /*DoSaveAs*/
  170.  
  171. /*----------*/
  172. static void DoSave (void);
  173. static void DoSave ()
  174. {
  175.     if (cur->fileNum == 0) {
  176.         DoSaveAs ();
  177.     } else {
  178.         SaveAppFile (cur->fileNum);
  179.     }
  180. } /*DoSave*/
  181.  
  182. /*----------*/
  183. static void CloseAppWindow (void);
  184. static void CloseAppWindow ()
  185. {
  186.     enum {saveItem = 1, cancelItem, discardItem};
  187.     
  188.     Str255            curTitle;
  189.     short            itemNum;
  190.     Boolean            okay;
  191.  
  192.     okay = true;
  193.     SetInfo (FrontWindow ());
  194.     if (cur->dirty) {
  195.         GetWTitle (curWindow, curTitle);
  196.         ParamText (curTitle, "", "", "");
  197.         InitCursor ();
  198.         itemNum = Alert (SaveID, nil);
  199.         switch (itemNum) {
  200.         case saveItem:
  201.                 DoSave ();
  202.                 okay = !errorFlag;
  203.             break;
  204.         case discardItem:
  205.                /*Do nothing*/;
  206.             break;
  207.         case cancelItem:
  208.                 errorFlag = true;
  209.                 okay = false;
  210.             break;
  211.         } /*switch*/
  212.     }
  213.     if (okay) {
  214.         if (cur->fileNum != 0) {
  215.             CloseAppFile (cur->fileNum);
  216.         }
  217.         CloseCurWindow ();
  218.     }
  219. } /*CloseAppWindow*/
  220.  
  221. /*----------*/
  222. void DoClose ()
  223. {
  224.     WindowPeek        frontPeek;
  225.  
  226.     errorFlag = false;
  227.  
  228.     frontPeek = (WindowPeek) FrontWindow ();
  229.     if (frontPeek->windowKind < 0) {
  230.         CloseDeskAcc (frontPeek->windowKind);
  231.     } else if (frontPeek->windowKind == dialogKind) {
  232.         CloseModelessDialog (FrontWindow ());
  233.     } else {
  234.         CloseAppWindow ();
  235.     }
  236. } /*DoClose*/
  237.  
  238. /*----------*/
  239. void DoQuit (void);
  240. void DoQuit ()
  241. {
  242.     Boolean            quitting;                             
  243.  
  244.     quitting = true;
  245.     while (quitting && (FrontWindow () != nil)) {
  246.         SystemTask ();
  247.         DoClose ();
  248.         if (errorFlag) {
  249.             quitting = false;
  250.         }
  251.     } /*while*/
  252.     
  253.     if (quitting) {
  254.         quittingTime = true;
  255.     }
  256. } /*DoQuit*/
  257.  
  258. /*----------*/
  259. static void DoRevert (void);
  260. static void DoRevert ()
  261. {
  262.     Str255            fileName;
  263.     Boolean            okay;
  264.  
  265.     okay = true;
  266.     if (cur->dirty) {
  267.         GetWTitle (curWindow, fileName);
  268.         ParamText (fileName, "", "", "");
  269.         okay = Confirm (RevertID);
  270.     }
  271.     if (okay) {
  272.         okay = ReadAppFile (cur->fileNum);
  273.     }
  274.     if (okay) {
  275.         InvalRect (&curWindow->portRect);
  276.     }
  277. } /*DoRevert*/
  278.  
  279. /*----------*/
  280. static void DoPageSetup (void);
  281. static void DoPageSetup ()
  282. {
  283. } /*DoPageSetup ()*/
  284.  
  285. /*----------*/
  286. static void DoPrint (void);
  287. static void DoPrint ()
  288. {
  289. } /*DoPrint ()*/
  290.  
  291. /*----------*/
  292. void DoFile (itemNr)
  293. short        itemNr;
  294. {
  295.     errorFlag = false;
  296.     
  297.     switch (itemNr) {
  298.     case FileNew:
  299.             DoNew ();
  300.         break;
  301.     case FileOpen:
  302.             DoOpen ();
  303.         break;
  304.     case FileClose:
  305.             DoClose ();
  306.         break;
  307.     case FileSave:
  308.             DoSave ();
  309.         break;
  310.     case FileSaveAs:
  311.             DoSaveAs ();
  312.         break;
  313.     case FileRevert:
  314.             DoRevert ();
  315.         break;
  316.     case FilePageSetup:
  317.             DoPageSetup ();
  318.         break;
  319.     case FilePrint:
  320.             DoPrint ();
  321.         break;
  322.     case FileQuit:
  323.             DoQuit ();
  324.         break;
  325.         } /*switch*/
  326. } /*DoFile*/
  327.  
  328. /* FileM */